<?php
class Dhcp_model extends MY_Model{
	var $hid;
	var $mac;
	var $ip;
	var $note;
	
	function __construct($id=''){
		parent::__construct();
		$GLOBALS['table'] = 'dhcp';
		if($id){
			$query	= $this->db->get_where('dhcp', array('id' => $id), 1);
			$result	= $query->result();
			if (!empty($result)){
				$this->id = $result[0]->id;
				$this->hid	= $result[0]->hid;
				$this->mac = $result[0]->mac;
				$this->ip = $result[0]->ip;
				$this->note = $result[0]->note;
			}
		}
	}
	
	function get_all_rows($where){
		if($where['key']){
			switch($where['options']){
				case 'host':
					$where = array('b.name'=>$where['key']);
					break;
				case 'mac':
					$where = array('a.mac'=>$where['key']);
					break;
				case 'ip':
					$where = array('a.ip'=>$where['key']);
					break;
				case 'name':
					$where = array('c.name'=>$where['key']);
					break;
			}
		}else{
			$where = array();
		}
		$this->db->select('a.id,b.name host,a.mac,a.ip,a.note,c.name ,c.department unit');
		$this->db->join('consumable b','a.hid=b.id','left');
		$this->db->join('staff c','b.sid=c.id','left');
		$this->db->like($where);
		$query = $this->db->get('dhcp a');
		return count($query->result_array());
	}
	
	function get_dhcp_by_page($limit,$offset,$where){
		if($where['key']){
			switch($where['options']){
				case 'host':
					$where = array('b.name'=>$where['key']);
					break;
				case 'mac':
					$where = array('a.mac'=>$where['key']);
					break;
				case 'ip':
					$where = array('a.ip'=>$where['key']);
					break;
				case 'name':
					$where = array('c.name'=>$where['key']);
					break;
			}
		}else{
			$where = array();
		}
		$this->db->select('a.id,b.name host,a.mac,a.ip,a.note,c.name ,c.department unit');
		$this->db->join('consumable b','a.hid=b.id','left');
		$this->db->join('staff c','b.sid=c.id','left');
		$this->db->like($where);
		$this->db->order_by('host','asc');
		$query = $this->db->get('dhcp a',$limit,$offset);
		
		return $query->result_array();
	}
	
	function download(){
		$this->db->select('c.name host,d.mac,d.ip');
		$this->db->join('consumable c','c.id=d.hid','left');
		$query = $this->db->get('dhcp d');
		return $query->result_array();
	}
}